package cn.ilikes.tools.hbase.jpa.configs.tools;

import java.io.InputStream;

import java.util.Properties;

import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.JPAHBaseRuntimeException;
import cn.ilikes.tools.hbase.jpa.metadatas.sub.configs.Datastorefactory;
import cn.ilikes.tools.hbase.jpa.metadatas.sub.configs.Indexfactory;
import cn.ilikes.tools.hbase.jpa.metadatas.sub.configs.IndexConfigs;
import cn.ilikes.tools.logs.Loggerfactory;

import com.thoughtworks.xstream.XStream;

public class PaserSubConfigs {

	private static Logger logger = Loggerfactory.instance(PaserSubConfigs.class);

	/**
	 * @param propertyfileName
	 * @return
	 */
	public static IndexConfigs parserIndexConfigs(String propertyfileName) {

		Loggerfactory.info(logger, "  parser  index configure ; file   : " + propertyfileName);
		try {
			InputStream inStream = ClassLoader.getSystemResourceAsStream(propertyfileName);
			if (inStream == null)
				inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertyfileName);
			if (inStream == null)
				throw new PaserConfigRuntimeException("not find index propertyfileName : " + propertyfileName + " inputStream  ");
			XStream stream = new XStream();
			stream.alias("indexconfigs", IndexConfigs.class);
			stream.alias("factory", Indexfactory.class);
			stream.alias("properties", Properties.class);
			Object o = stream.fromXML(inStream);
			return (IndexConfigs) o;
		} catch (Exception e) {
			throw new JPAHBaseRuntimeException(e);
		}
	}

	public static Datastorefactory parsedataStoreFactory(String propertyfileName) {

		Loggerfactory.info(logger, "parser  dataStore configure  file name : " + propertyfileName);
		InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertyfileName);
		if (inStream == null)
			inStream = ClassLoader.getSystemResourceAsStream(propertyfileName);
		if (inStream == null)
			throw new PaserConfigRuntimeException("not find dataStore  propertyfileName : " + propertyfileName + " inputStream  ");
		XStream stream = new XStream();
		stream.alias("datastorefacpory", Datastorefactory.class);
		stream.alias("dataStore", Datastorefactory.DataStore.class);
		stream.alias("schema", Datastorefactory.DataStore.Schema.class);
		stream.alias("table", Datastorefactory.DataStore.Schema.Table.class);
		stream.alias("dataCenter", Datastorefactory.DataStore.Schema.DataCenter.class);
		stream.alias("connection", Datastorefactory.DataStore.Connection.class);
		stream.alias("server", Datastorefactory.DataStore.Connection.Server.class);
		Object o = stream.fromXML(inStream);
		return (Datastorefactory) o;
	}

}
